package com.lcredu.lcrliving.commodity.service.impl;

import com.lcredu.common.util.PageUtils;
import com.lcredu.common.util.Query;
import com.lcredu.lcrliving.commodity.entity.AttrEntity;
import com.lcredu.lcrliving.commodity.service.AttrService;
import com.lcredu.lcrliving.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lcredu.lcrliving.commodity.dao.AttrgroupDao;
import com.lcredu.lcrliving.commodity.entity.AttrgroupEntity;
import com.lcredu.lcrliving.commodity.service.AttrgroupService;

import javax.annotation.Resource;


@Service("attrgroupService")
public class AttrgroupServiceImpl extends ServiceImpl<AttrgroupDao, AttrgroupEntity> implements AttrgroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                new QueryWrapper<AttrgroupEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 增加根据分类(第3级分类)+查询条件[key,categoryId]+分页的API接口
     *
     * @param params
     * @param categoryId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //先获取到检索传入的关键字
        String key = (String) params.get("key");

        //根据实际情况，封装查询条件到QueryWrapper对象
        QueryWrapper<AttrgroupEntity> wrapper = new QueryWrapper<>();

        //判断key是否含有查询条件(非空？)-希望它是一组独立的检索条件
        if (StringUtils.isNotBlank(key)) {
            //id就是相等条件,name就是模糊查询
            //wrapper.eq("id", key).or().like("name", key);
            wrapper.and((obj) -> {
                obj.eq("id", key).or().like("name", key);
            });
        }

        //处理是否需要封装categoryId检索条件
        //先设置一个规则:categoryId==0表示在查询属性组的时候,不加入categoryId
        //否则我就加入 AND categoryId = ? 逻辑需要前端代码配合
        if (categoryId != 0) {
            wrapper.eq("category_id", categoryId);
        }
        IPage<AttrgroupEntity> page = this.page(
                new Query<AttrgroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Resource
    private AttrService attrService;

    /**
     * 根据分类categoryId返回该分类关联的属性组和这些属性组关联的基本属性
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCategoryId(Long categoryId) {
        //1.根据categoryId得到该分类关联的所有属性组
        List<AttrgroupEntity> attrgroupEntities = this.list(new QueryWrapper<AttrgroupEntity>().eq("category_id", categoryId));
        //2.根据前面查询到的"attrgroupEntities",对其进行遍历,将各个属性组关联的基本属性查询到,封装到AttrGroupWithAttrsVo集合然后返回
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrgroupEntities.stream().map(attrgroupEntity -> {
            //(1)创建一个AttrGroupWithAttrsVo对象
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //(2)将attrgroupEntity对象属性拷贝
            BeanUtils.copyProperties(attrgroupEntity, attrGroupWithAttrsVo);
            //(3)通过属性组id获取到属性组关联的基本属性
            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupWithAttrsVo.getId());
            //(4)将前面得到的attrEntities设置到AttrGroupWithAttrsVo的attrs属性
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrsVos;
    }


}
